package edu.ecu.seng6270.goa.ropa;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;

import net.miginfocom.swing.MigLayout;
import edu.ecu.seng6270.goa.pss.PSSExternal;

/**
 * GUI Panel to prompt user for login credentials
 */
public class LoginPanel extends JPanel{
	private static final long serialVersionUID = -7516349402901609944L;

	private JTextField customerID;
	private JLabel loginMessage;
	private ROPAMain parent;
	
	LoginPanel (final ROPAMain ropaMain) {
		super();
		
		this.parent = ropaMain;
		
		//////////////////////////////////////
		//			Login Panel				//
		//////////////////////////////////////
		this.setBackground(new Color(255, 255, 255));
		this.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		MigLayout ml_loginPanel = new MigLayout("", "[grow]", "[][][][]");
		this.setLayout(ml_loginPanel);
		
		JLabel lblPleaseEnterYour = new JLabel("Please enter your Customer ID:");
		lblPleaseEnterYour.setHorizontalAlignment(SwingConstants.CENTER);
		this.add(lblPleaseEnterYour, "cell 0 0,alignx center");
		
		customerID = new JTextField();
		this.add(customerID, "cell 0 1,alignx center");
		customerID.setColumns(10);
		
		JButton loginButton = new JButton("Login");
		this.add(loginButton, "cell 0 2,alignx center");
		
		loginMessage = new JLabel("Your Customer ID is a 6 characters (letters and numbers) located on the top of your VPL receipt");
		loginMessage.setForeground(new Color(0, 128, 0));
		loginMessage.setFont(new Font("Tahoma", Font.PLAIN, 11));
		this.add(loginMessage, "cell 0 3,alignx center");
		
		loginButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int loginStatus = PSSExternal.validateUserID(customerID.getText());
				switch (loginStatus) {
				case 2:
					parent.moveToCard("existingOrderPanel");
					break;
				case 3:
					parent.setMyCustomerID(customerID.getText());
					loginMessage.setText("Welcome Back!");
					loginMessage.setForeground(new Color(0, 128, 0));
					parent.moveToCard("orderTypePanel");
					break;
				default:
					loginMessage.setText("The Customer ID you entered is invalid, please try again");
					loginMessage.setForeground(new Color(128, 0, 0));
					break;
				}
			}
		});

		
	}

}
